package com.example.ceramics.database.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.ceramics.database.DatabaseContants;
import com.example.ceramics.database.DatabaseHelper;
import com.example.ceramics.entity.UserEntity;

import java.util.ArrayList;
import java.util.List;

public class UserDao {

    private final DatabaseHelper helper;

    public UserDao(Context context) {
        helper = new DatabaseHelper(context);
    }

    public void insert(String account, String password, String name, String nickname, int phone, String email,
                       String addressProvince, String addressCity, String addressCounty, String addressDetail) {
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql = "insert into " + DatabaseContants.TABLE_USER + " values(?,?,?,?,?,?,?,?,?,?)";
        db.execSQL(sql, new Object[]{account, password, name, nickname, phone, email,
                addressProvince, addressCity, addressCounty, addressDetail});
        db.close();
    }

    public void delete(String account) {
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql = "delete from " + DatabaseContants.TABLE_USER + " where account = " + account;
        db.execSQL(sql);
        db.close();
    }

    public void updateAddress(String account, String name, String nickname, int phone, String email,
                              String addressProvince, String addressCity, String addressCounty, String addressDetail) {
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql = "update " + DatabaseContants.TABLE_USER +
                " set name=?,phone=?,addressProvince=?,addressCity=?,addressCounty=?,addressDetail=? where account=?";
        db.execSQL(sql, new Object[]{name, phone, addressProvince, addressCity, addressCounty, addressDetail, account});
        db.close();
    }

    public void updateInfo(String account,String nickname,int phone,String email,String img){
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql = "update " + DatabaseContants.TABLE_USER +
                " set nickname=?,phone=?,email=?,img=? where account=?";
        db.execSQL(sql, new Object[]{nickname, phone, email, img,account});
        db.close();
    }

    public List<UserEntity> query() {
        SQLiteDatabase db = helper.getReadableDatabase();
        String sql = "select * from "+DatabaseContants.TABLE_USER;
        Cursor cursor = db.rawQuery(sql, null);
        List<UserEntity> users = new ArrayList<>();
        while (cursor.moveToNext()){
            String account = cursor.getString(cursor.getColumnIndex("account"));
            String password = cursor.getString(cursor.getColumnIndex("password"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String nickname = cursor.getString(cursor.getColumnIndex("nickname"));
            String phone = cursor.getString(cursor.getColumnIndex("phone"));
            String email = cursor.getString(cursor.getColumnIndex("email"));
            String addressProvince = cursor.getString(cursor.getColumnIndex("addressProvince"));
            String addressCity = cursor.getString(cursor.getColumnIndex("addressCity"));
            String addressCounty = cursor.getString(cursor.getColumnIndex("addressCounty"));
            String addressDetail = cursor.getString(cursor.getColumnIndex("addressDetail"));
            String img = cursor.getString(cursor.getColumnIndex("img"));
            UserEntity userEntity = new UserEntity(account,password,name,nickname,phone,email,addressProvince,addressCity,addressCounty,addressDetail,img);
            users.add(userEntity);
        }
        db.close();
        return users;
    }
}
